package com.czy.owner.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.HotShopClassifyModel;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.glide.GlideUtils;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

/**
 * Created by asus on 2017/5/13.
 */
public class HotShopClassifyRightAdapter extends RecyclerArrayAdapter<HotShopClassifyModel.SubsBean> {
    private static final int GROUP_TYPE = 0;
    private static final int CLASSIFY_TYPE = 1;



    public HotShopClassifyRightAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == GROUP_TYPE) {
            return new HotShopClassifyHeaderHolder(parent);
        } else if (viewType == CLASSIFY_TYPE) {
            return new HotShopClassifyHolder(parent);
        }
        return null;
    }


    @Override
    public void OnBindViewHolder(BaseViewHolder holder, int position) {
        super.OnBindViewHolder(holder, position);
    }

    @Override
    public int getViewType(int position) {
        if (getItem(position).isGroup()) {
            return GROUP_TYPE;
        } else {
            return CLASSIFY_TYPE;
        }

    }

    public TipSpanSizeLookUp obtainTipSpanSizeLookUp() {
        return new TipSpanSizeLookUp();
    }

    public class TipSpanSizeLookUp extends GridSpanSizeLookup {
        public TipSpanSizeLookUp() {
            super(3);
        }

        @Override
        public int getSpanSize(int position) {
            if (position < getHeaderCount() || position >= getCount() + getHeaderCount()) {
                return 3;
            } else {
                if (getItem(position-getHeaderCount()).isGroup()) {
                    return 3;
                } else {
                    return 1;
                }
            }
        }
    }

    class HotShopClassifyHolder extends BaseViewHolder<HotShopClassifyModel.SubsBean> {
        private TextView tvClassify;
        private ImageView ivClassify;

        public HotShopClassifyHolder(ViewGroup parent) {
            super(parent, R.layout.item_classify_right);
            tvClassify = $(R.id.tv_classify_item);
            ivClassify = $(R.id.iv_classify);
        }

        @Override
        public void setData(HotShopClassifyModel.SubsBean model) {
            super.setData(model);
            tvClassify.setText(model.getCategoryName());
            GlideUtils.loadImage(getContext(), model.getLogo(), ivClassify, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_icon, R.mipmap.default_icon);
        }
    }

    class HotShopClassifyHeaderHolder extends BaseViewHolder<HotShopClassifyModel.SubsBean> {
        private TextView tvClassifyHeader;

        public HotShopClassifyHeaderHolder(ViewGroup parent) {
            super(parent, R.layout.item_classify_head);
            tvClassifyHeader = $(R.id.tv_classify_head_item);
        }

        @Override
        public void setData(HotShopClassifyModel.SubsBean model) {
            super.setData(model);
            tvClassifyHeader.setText(model.getCategoryName());
        }
    }

}

